package com.cndw.kungfu.model.activity.holiday;

import java.text.ParseException;
import java.util.Map;

import com.cndw.kungfu.ext.LogUtil;
import com.cndw.kungfu.ext.TimeProcesser;
import com.cndw.kungfu.model.ActivitySerialize;
import com.cndw.kungfu.model.Context;

public class ZongZiSteamer {

	public static final int begintTime;
	public static final int endTime;

	private static final int startFirst = 11 * 3600;

	private static final int startSecond = 20 * 3600 + 1800;

	private static final int onceTime = 7200;

	private static final Map<Long, Integer> addPlayersMap;

	private static int lifeDel = 0;

	private static int step = 0;

	private static int currentStartTime = 0;

	private static int lastStartTime = 0;

	static {
		int tmpBegin = 0, tmpEnd = 0;
		try {
			tmpBegin = TimeProcesser.getUnixTimeByString("2012-06-19 07:00:00");
			tmpEnd = TimeProcesser.getUnixTimeByString("2012-06-26 07:00:00");
		} catch (ParseException e) {
			e.printStackTrace();
		}
		addPlayersMap = ActivitySerialize.getInstance().zongziFireMapGet();

		begintTime = tmpBegin;
		endTime = tmpEnd;
	}

	public static void runBoss() {
		int morningTime = TimeProcesser.getMorningTime();
		int currentTime = TimeProcesser.getUnixTime();
		if (currentTime >= endTime) {// 活动结束了
			return;
		}
		int diffTime = currentTime - morningTime;
		int nextTime = 0;
		if (diffTime <= startFirst || diffTime < startFirst + onceTime - 120) {
			nextTime = startFirst;
		} else if (diffTime <= startSecond || diffTime < startSecond + onceTime - 120) {
			nextTime = startSecond;
		} else {
			nextTime = startFirst + 86400;
		}
		currentStartTime = nextTime + morningTime;
		LogUtil.getMain().debug("zongzi start " + TimeProcesser.getDateByUnixTime(currentStartTime));
		Context.getTimerServer().scheduleSeconds(new Runnable() {
			@Override
			public void run() {
				init();
				LogUtil.getMain().debug("zongzi start ...");
			}
		}, nextTime - diffTime);
		LogUtil.getMain().debug("zongzi start in delay  " + (nextTime - diffTime));
	}

	private static void init() {
		if (step != 0) {
			addPlayersMap.clear();
		}
		lifeDel = 0;
		step = 1;
		lastStartTime = currentStartTime;
		Context.getTimerServer().scheduleSeconds(new Runnable() {
			@Override
			public void run() {
				step = 2;
				runBoss();
				LogUtil.getMain().debug("zongzi award ok! ");
			}
		}, currentStartTime + onceTime - TimeProcesser.getUnixTime());

		LogUtil.getMain().debug("zongzi award delay " + (currentStartTime + onceTime - TimeProcesser.getUnixTime()));
	}

	public static int getStep() {
		return step;
	}

	public static int getAward(long playerId) {
		if (!addPlayersMap.containsKey(playerId)) {
			return 0;
		} else {
			return addPlayersMap.get(playerId);
		}
	}

	public static int getCurrentEndTime() {
		if (step == 2) {
			return lastStartTime + onceTime;
		}
		return currentStartTime + onceTime - lifeDel;
	}

	public static int getCurrentStartTime() {
		return currentStartTime;
	}

	public static int getActivityEndTime() {
		return endTime;
	}

	public static boolean canFire() {
		return step == 1 && TimeProcesser.getUnixTime() >= currentStartTime;
	}

	public static boolean canGot() {
		return step == 2 || TimeProcesser.getUnixTime() >= getCurrentEndTime();
	}

	public static void awardAlter(long uid, int awardNum) {
		addPlayersMap.put(uid, awardNum);
	}

	public static void fireAdd(long uid, int time) {
		lifeDel += time;
	}

}
